package com.wannengbang.cloudleader.homepage;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.RecyclerView;

import com.ethanhua.skeleton.Skeleton;
import com.ethanhua.skeleton.SkeletonScreen;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseFragment;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.QuotaListBean;
import com.wannengbang.cloudleader.homepage.adapter.TeamDetailsListAdapter;
import com.wannengbang.cloudleader.homepage.model.HomePageModelImpl;
import com.wannengbang.cloudleader.mine.adapter.ByOrderListAdapter;
import com.wannengbang.cloudleader.widget.CommonNoTitleDialog;
import com.wannengbang.cloudleader.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 我的订单
 */
public class TeamDetailsFragment extends BaseFragment implements OnRefreshLoadMoreListener {
    public static final String ORDER_TITLE_NAME = "title";
    public static final String TEAM_ID = "team_id";
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    Unbinder unbinder;
    private String titleName;
    private String type;
    private HomePageModelImpl homePageModel;
    private int page = 1;
    private List<QuotaListBean.DataBean.ItemListBean> beanList;
    private TeamDetailsListAdapter listAdapter;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    private SkeletonScreen skeletonScreen;
    private String team_id;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_team_details_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        Bundle bundle = getArguments();
        if (bundle != null) {
            titleName = bundle.getString(ORDER_TITLE_NAME);
            team_id = bundle.getString(TEAM_ID);
            type = getType();
        }
        initView();
        initData();
        return view;
    }

    private void initView() {
        refreshLayout.setRefreshHeader(new ClassicsHeader(mActivity));//设置Header
        refreshLayout.setRefreshFooter(new ClassicsFooter(mActivity));//设置Footer
        refreshLayout.setOnRefreshLoadMoreListener(this);
    }

    private void initData() {
        homePageModel = new HomePageModelImpl();
        beanList = new ArrayList<>();
        listAdapter = new TeamDetailsListAdapter(beanList);
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(mActivity));
        recyclerView.setAdapter(listAdapter);
        skeletonScreen = Skeleton.bind(recyclerView)
                .adapter(listAdapter)
                .color(R.color.shimmer_color_2)
                .shimmer(true)
                .angle(20)
                .frozen(false)
                .duration(1000)
                .count(20)
                .load(R.layout.item_skeleton_product_list)
                .show();
        requestData();
    }


    public void requestData() {
        homePageModel.requestTeamRewardInfo(page, team_id, type, new DataCallBack<QuotaListBean>() {
            @Override
            public void onSuccessful(QuotaListBean bean) {
                if (bean.getData().getItemList() != null && bean.getData().getItemList().size() > 0) {
                    beanList.addAll(bean.getData().getItemList());
                    if (page == 1) {
                        refreshLayout.finishRefresh();
                    } else {
                        refreshLayout.finishLoadMore();
                    }
                } else {
                    if (page != 1) {
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    } else {
                        refreshLayout.finishRefresh();
                    }
                }
                if (beanList.size() > 0) {
                    llNoData.setVisibility(View.GONE);
                } else {
                    llNoData.setVisibility(View.VISIBLE);
                }
                listAdapter.notifyDataSetChanged();
                if (page == 1) {
                    skeletonScreen.hide();
                }
            }

            @Override
            public void onFailed(String msg, String code) {
                listAdapter.notifyDataSetChanged();
                if (page == 1) {
                    skeletonScreen.hide();
                }
            }
        });
    }

    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {
        page++;
        requestData();
        refreshLayout.finishLoadMore(30000);
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        page = 1;
        beanList.clear();
        requestData();
        refreshLayout.finishRefresh(30000);
    }

    private String getType() {
        String status = "0";
        if (titleName.contains("全部")) {
            status = "0";
        } else if (titleName.contains("已完成")) {
            status = "1";
        } else if (titleName.contains("未完成")) {
            status = "2";
        }
        return status;
    }

}
